package xworker.swt.dataObject;

import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.widgets.*;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.swt.app.view.swt.widgets.PagingToolbar;
import xworker.dataObject.DataStore;
import xworker.dataObject.DataStoreListener;
import xworker.swt.dataObject.datastore.*;

public class SwtDataStore {
    private static final String KEY = "SwtDataStoreListener";

    public static Object create(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        DataStore dataStore = new DataStore(self, actionContext);
        actionContext.g().put(self.getMetadata().getName(), dataStore);

        String binds = self.doAction("getBindTo", actionContext);
        if(binds != null){
            for(String bind : binds.split("[,]")){
                bind = bind.trim();

                if(bind.isEmpty()){
                    continue;
                }

                int index = bind.indexOf("?");
                String params = null;
                if(index != -1){
                    params = bind.substring(index + 1);
                    bind = bind.substring(0, index);
                }
                Object obj = actionContext.getObject(bind);
                bind(dataStore, obj, params, actionContext);
            }
        }

        return dataStore;
    }

    public static Object bind(DataStore dataStore, Object object, String params,  ActionContext actionContext){
        Object result = null;
        if(object instanceof DataStoreListener){
            dataStore.addListener((DataStoreListener) object);
            result = object;
        }else if(object instanceof Table){
            result = new TableDataStoreListener((Table) object, dataStore, actionContext);
        }else if(object instanceof CCombo){
            result =  new CComboDataStoreListener((CCombo) object, dataStore, actionContext);
        }else if(object instanceof List){
            result =  new ListDataStoreListener((List) object, dataStore, actionContext);
        }else if(object instanceof Thing){
            PagingToolbar pagingToolbar = PagingToolbar.getPagingToolbar((Thing) object);
            if(pagingToolbar != null){
                result =  new PagingToolbarDataStoreListener(pagingToolbar, dataStore, actionContext);
            }
        }else if(object instanceof Combo){
            result = new ComboDataStoreListener((Combo) object, dataStore, actionContext);
        }else if(object instanceof Composite){
            result =  new CheckBoxDataStoreListener((Composite) object, dataStore, "radio".equals(params), actionContext);
        }

        if(object instanceof Control && result instanceof ControlDataStoreListener){
            ((Control) object).setData(KEY, result);
        }

        return result;
    }

    public static ControlDataStoreListener getDataStoreListener(Control control){
        return (ControlDataStoreListener) control.getData(KEY);
    }

}
